add_subdirectory(glossary)

# Set of available languages:
# * managed at the Translation Project (TP):
set (TP_LINGUAS az ca cs da eu fa ja nl ro rw sk sr sv tr uk zh_CN)
# * already marked as external at TP:
set (GC_LINGUAS ar bg de el en_GB es fi fr gu he hi hu it kn ko lt lv mr nb ne pl pt pt_BR ru ta te ur vi zh_TW)
# * New or unmarked: The release manager should announce them to TP 
# and when listed there move in the respective group above.
set (NEW_LINGUAS as brx doi es_NI hr kok kok@latin ks mai mni mni@bengali)

set (ALL_LINGUAS ${TP_LINGUAS} ${GC_LINGUAS} ${NEW_LINGUAS})

file (WRITE LINGUAS "${ALL_LINGUAS}")

set (CATALOGS "")
set (BUILD_CATALOGS "")

set(po_SOURCES "")
file (WRITE LINGUAS "")
foreach(lingua ${ALL_LINGUAS})
  list(APPEND po_SOURCES ${lingua}.po)
  file (APPEND LINGUAS "${lingua} ")
endforeach()

set_local_dist(po_DIST_local ${po_SOURCES} CMakeLists.txt Makevars
  POTFILES.in POTFILES.skip README gnucash-pot.cmake)
set(po_DIST ${po_DIST_local} ${po_glossary_DIST} PARENT_SCOPE)

foreach(lingua ${ALL_LINGUAS})
  set(_OUTPUT_FILE ${lingua}.mo)
  list(APPEND CATALOGS ${_OUTPUT_FILE})
  add_custom_command(
      OUTPUT ${_OUTPUT_FILE}
      COMMAND ${CMAKE_COMMAND} -E env
        ${GETTEXT_MSGFMT_EXECUTABLE}
            -o ${_OUTPUT_FILE} ${CMAKE_CURRENT_SOURCE_DIR}/${lingua}.po
      DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/${lingua}.po
  )
  set(_BUILD_FILE_DIR ${DATADIR_BUILD}/locale/${lingua}/LC_MESSAGES)
  make_directory(${_BUILD_FILE_DIR})
  set(_BUILD_FILE ${_BUILD_FILE_DIR}/gnucash.mo)
  list(APPEND BUILD_CATALOGS ${_BUILD_FILE})
  add_custom_command(
    OUTPUT ${_BUILD_FILE}
    COMMAND ${CMAKE_COMMAND} -E copy ${CMAKE_CURRENT_BINARY_DIR}/${lingua}.mo ${_BUILD_FILE}
    DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/${_OUTPUT_FILE}
    #APPEND
    )
endforeach(lingua)

add_custom_target(po-gmo ALL DEPENDS ${CATALOGS})
add_custom_target(po-gmo-build ALL DEPENDS ${BUILD_CATALOGS})


foreach(lingua ${ALL_LINGUAS})
  install(FILES ${CMAKE_CURRENT_BINARY_DIR}/${lingua}.mo RENAME gnucash.mo DESTINATION ${CMAKE_INSTALL_DATADIR}/locale/${lingua}/LC_MESSAGES)
endforeach(lingua)

function(read_file_removing_comments OUTPUT PATH)
  set(RESULT "")
  file(STRINGS "${PATH}" LINES_IN)
  foreach(line ${LINES_IN})
    string(REGEX REPLACE "#.*" "" line2 ${line})
    list(APPEND RESULT ${line2})
  endforeach()
  set(${OUTPUT} ${RESULT} PARENT_SCOPE)
endfunction()


function(make_gnucash_potfiles)
  # Create a list of candidate translation files
  file (GLOB_RECURSE FILES_IN RELATIVE ${CMAKE_SOURCE_DIR}
    ${CMAKE_SOURCE_DIR}/*.c ${CMAKE_SOURCE_DIR}/*.cpp
    ${CMAKE_SOURCE_DIR}/*.glade ${CMAKE_SOURCE_DIR}/*.desktop.in.in
    ${CMAKE_SOURCE_DIR}/*.gschema.xml.in ${CMAKE_SOURCE_DIR}/*.appdata.xml.in.in
    ${CMAKE_SOURCE_DIR}/*.keys.in ${CMAKE_SOURCE_DIR}/*.scm
    ${CMAKE_SOURCE_DIR}/*/qofbookslots.h
  )

  # Only consider files in a selection of the source directories and
  # additionally check against list of ignore patterns
  set (GOOD_FILES "")
  foreach (path ${FILES_IN})
    if (${path} MATCHES "^(bindings/|borrowed/|common/|doc/|libgnucash/|gnucash/)"
        AND
        NOT ${path} MATCHES "gw-|test|experimental|python-bindings|swig-.*\\.c"
        # Skip POSIX style hidden files even if they have the proper extension
        # These are typically temporary files from editors like emacs
        AND
        NOT ${path} MATCHES "/[.][^/]*$")
        list (APPEND GOOD_FILES ${path})
    endif ()
  endforeach (path)

  # Remove the paths that we have marked as explicitly skipped
  read_file_removing_comments(SKIP_LINES POTFILES.skip)
  foreach (path ${SKIP_LINES})
      list(REMOVE_ITEM GOOD_FILES ${path})
  endforeach ()


  # CMake sorting is different from UNIX sorting. Use perl to
  # sort POTFILES.in universally. This may no longer be needed
  # now we have dropped autotools support.
  string(REPLACE ";" "\n" SORT_IN "${GOOD_FILES}")
  set(SORT_IN "${SORT_IN}\n")
  file(WRITE ${CMAKE_CURRENT_BINARY_DIR}/POTFILES.in.in "${SORT_IN}")

  execute_process(COMMAND "${PERL_EXECUTABLE}" "${CMAKE_SOURCE_DIR}/util/elegant-sort.pl"
    INPUT_FILE ${CMAKE_CURRENT_BINARY_DIR}/POTFILES.in.in
    OUTPUT_VARIABLE POTFILES_IN
  )
  string(REPLACE "\n" ";" POTFILES "${POTFILES_IN}")

  # Write out the final list.
  # intltool-update insists that this file be in the source directory. :-(
  set(POTFILES_IN_PATH ${CMAKE_CURRENT_BINARY_DIR}/POTFILES.in)
  file(WRITE ${POTFILES_IN_PATH} "# This is a list of files which contain translatable strings.
# This file was autogenerated by cmake.
")

  set(POTFILE_DEPS "")
  foreach(path ${POTFILES})
    list(APPEND POTFILE_DEPS ${CMAKE_SOURCE_DIR}/${path})
    file(APPEND ${POTFILES_IN_PATH} "${path}\n")
  endforeach()

  configure_file(${POTFILES_IN_PATH} ${CMAKE_CURRENT_SOURCE_DIR}/POTFILES.in NEWLINE_STYLE LF)

  set(gnucash_pot_depends ${POTFILE_DEPS}  CACHE INTERNAL "List of files with translatable strings. If any of these change, gnucash.pot should be regenerated")
endfunction()

if(BUILDING_FROM_VCS)

  make_gnucash_potfiles()

  find_program(XGETTEXT xgettext)
  configure_file (${CMAKE_CURRENT_SOURCE_DIR}/Makevars
    ${CMAKE_CURRENT_BINARY_DIR}/Makevars COPYONLY)

  if (${XGETTEXT} STREQUAL "XGETTEXT-NOTFOUND")
    message(FATAL_ERROR "Can't find the 'xgettext' program.")
  endif ()

  add_custom_command(OUTPUT gnucash.pot
        COMMAND ${CMAKE_COMMAND}
           -D TOP_SRC_DIR=${CMAKE_SOURCE_DIR}
           -D PO_SRC_DIR=${CMAKE_CURRENT_SOURCE_DIR}
           -D PO_BIN_DIR=${CMAKE_CURRENT_BINARY_DIR}
           -D PACKAGE=${PACKAGE}
           -D PACKAGE_NAME=${PACKAGE_NAME}
           -D PACKAGE_VERSION=${PACKAGE_VERSION}
           -D XGETTEXT=${XGETTEXT}
           -P ${CMAKE_CURRENT_SOURCE_DIR}/gnucash-pot.cmake
        DEPENDS ${gnucash_pot_depends}
        WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
  )
  add_custom_target (pot DEPENDS gnucash.pot)
endif()
dist_add_generated (${BUILDING_FROM_VCS} gnucash.pot)
